package cn.lanqiao.blog.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;

import com.github.pagehelper.PageInfo;

import cn.lanqiao.blog.entity.Article;
import cn.lanqiao.blog.service.ArticleServicle;
import cn.lanqiao.blog.utils.Result;
import cn.lanqiao.blog.vo.ArticleVO;
@CrossOrigin
@RestController
@RequestMapping("article")
public class ArticleController {

	@Autowired
	private ArticleServicle articleService;
	
	//登录后首页文章
	@RequestMapping("getAllVisible")
	public Result getAllVisible(Integer userId, Integer page, Integer limit, String title, String username, Integer lableId) {
		PageInfo<ArticleVO> pageInfo = articleService.getAllVisible(userId, page, limit, title, username, lableId);
		int total = articleService.getAllVisibleTotal(userId, title, username, lableId);
		return Result.success(200, "", pageInfo, total);
	}
	
	//我的关注
	@RequestMapping("getFollow")
	public Result getFollow(Integer userId, Integer page, Integer limit, String title, String username, Integer lableId) {
		PageInfo<ArticleVO> pageInfo = articleService.getFollow(userId, page, limit, title, username, lableId);
		int total = articleService.getMyFollowTotal(userId, title, username, lableId);
		return Result.success(200, "", pageInfo, total);
	} 
	
	//我的文章
	@RequestMapping("getMyArticle")
	public Result getMyArticle(Integer userId, Integer page, Integer limit, String title, Integer lableId) {
		PageInfo<ArticleVO> pageInfo =  articleService.getMyArticle(userId, page, limit, title, lableId);
		int total = articleService.getMyArticleTotal(userId, title, lableId);
		return Result.success(200, "", pageInfo, total);
	} 
	
	//我的收藏夹
	@RequestMapping("getCollectionList")
	public Result getCollectionList(Integer userId, Integer page, Integer limit, String title,  String username, Integer lableId) {
		PageInfo<ArticleVO> pageInfo = articleService.getCollectionList(userId, page, limit, title, username, lableId);
		int total = articleService.getMyCollectionTotal(userId, title, username, lableId);
		return Result.success(200, "", pageInfo, total);
	}
	
	//文章评论数
	@RequestMapping("getArticleCommentCount")
	public int getArticleCommentCount(Integer articleId) {
		return articleService.getArticleCommentCount(articleId);
	}
	
	//新增点赞
	@RequestMapping("increasePraise")
	public Result increasePraise(Integer articleId) {
		return Result.success(articleService.increasePraise(articleId));
	}
	
	//点赞减少
	@RequestMapping("decreasePraise")
	public Result decreasePraise(Integer articleId) {
		return Result.success(articleService.decreasePraise(articleId));
	}
	
	//新增浏览量
	@RequestMapping("increaseViews")
	public int increaseViews(Integer articleId) {
		return articleService.increaseViews(articleId);
	}
	
	
	//添加到收藏夹
	@RequestMapping("addCollection")
	public Result addCollection(Integer userId, Integer articleId) {
		return Result.success(articleService.addCollection(userId, articleId));
	}
	
	//从收藏夹中删除
	@RequestMapping("deleteCollection")
	public Result deleteCollection(Integer userId, Integer articleId) {
		return Result.success(articleService.deleteCollection(userId, articleId));
	}
	
	//进入他人主页后显示的文章列表
	@RequestMapping("showAdminArticle")
	public Result showAdminArticle(Integer adminId, Integer viewerId, Integer page, Integer limit, String title, Integer lableId) {
		PageInfo<ArticleVO> pageInfo = articleService.showAdminArticle(adminId, viewerId, page, limit, title, lableId);
		if(articleService.isAdminFans(adminId, viewerId)) {
			int total = articleService.getAdminFansTotal(adminId, title, lableId);
			return Result.success(200, "", pageInfo, total);
		}else {
			int total = articleService.getNotAdminFansTotal(adminId, title, lableId);
			return Result.success(200, "", pageInfo, total);
		}
	}  
	
	//修改可见性权限
	@RequestMapping("changeVisible")
	public int changeVisible(Integer articleId, Integer visible) {
		return articleService.changeVisible(articleId, visible);
	}
	
	//修改可评论权限
	@RequestMapping("changeCommentable")
	public int changeCommentable(Integer articleId, Integer commentable) {
		return articleService.changeCommentable(articleId, commentable);
	}
	
	//游客首页文章列表
	@RequestMapping("getTouristVisible")
	public Result getTouristVisible(Integer page, Integer limit, String title, String username, Integer lableId){
		PageInfo<ArticleVO> pageInfo =  articleService.getTouristVisible(page, limit, title, username, lableId);
		int total = articleService.getTouristTotal(title, username, lableId);
		return Result.success(200, "", pageInfo, total);
	}
	
	//写文章
	@RequestMapping("writeArticle")
	public Result writeArticle(Integer userId, Integer articleId, String title, String abs, String text, 
			Integer visible, Integer commentable, Integer[] lableIds) {
		Article article = new Article();
		article.setTitle(title);
		article.setFile(abs);
		article.setText(text);
		article.setVisible(visible);
		article.setCommentable(commentable);
		article.setUserId(userId);
		if(articleService.writeArticle(article, lableIds) == 1) {
			return Result.success(200, "提交成功，文章正在审核", 1);
		}else {
			return Result.success(200, "文章已发表", 0);
		}
		
	}
	
	@RequestMapping("getArticleDetail")
	public Result getArticleDetail(Integer articleId) {
		return Result.success(articleService.getArticleDetail(articleId));
	}
	
	@RequestMapping("editArticle")
	public Result editArticle(Integer articleId, String title, String abs, String text, 
			Integer visible, Integer commentable, Integer[] lableIds) {
		Article article = new Article();
		System.out.println(visible);
		article.setId(articleId);
		article.setTitle(title);
		article.setFile(abs);
		article.setText(text);
		article.setVisible(visible);
		article.setCommentable(commentable);
		int result = articleService.editArticle(article, lableIds);
		if(result == 1) {
			return Result.success(result);
		}else {
			return Result.fail("编辑失败");
		}
		
	}
	
	@RequestMapping("praiseArticle")
	public Result praiseArticle(Integer userId, Integer articleId) {
		return Result.success(articleService.praiseArticle(userId, articleId));
	}
	
	@RequestMapping("delPraise")
	public Result delPraise(Integer userId, Integer articleId) {
		return Result.success(articleService.delPraise(userId, articleId));
	}
	
	@RequestMapping("/getpage")
	public Result getPage(@RequestParam("page")Integer pageNum,@RequestParam("limit")Integer pageSize,Article article){
		PageInfo<ArticleVO> page = articleService.getArticlePage(pageNum, pageSize,article);
		return Result.success(page);
	}
	@RequestMapping("/getpageno")
	public Result getnoPage(@RequestParam("page")Integer pageNum,@RequestParam("limit")Integer pageSize,Article article){
		PageInfo<ArticleVO> page = articleService.getNoArticlePage(pageNum, pageSize, article);	
		return Result.success(page);
	}
	
	@RequestMapping("/delete")
	public Result delete(Integer id) {
		 int a = articleService.deleteArticle(id);
		 return Result.success(a);
	}
	//审核
	@RequestMapping("/update")
	public Result update(Article article) {
		int a = articleService.updateArticle(article);
		return Result.success(a);
	}
	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
		
		//转换日期
		DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
	}
}
